package com.hmall.search.mq;

import com.hmall.search.constans.ItemMqConstants;
import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.ElementType;

/**
 * @Author: ahcfl
 * @Date: 2021年07月24日 22:27
 * @Description: 监听mq消息  同步数据
 */
@Component
public class ItemListener {

    @Autowired
    private ISearchService searchService;

    // 监听 添加 修改
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ItemMqConstants.INSERT_QUEUE_NAME),
            exchange = @Exchange(name = ItemMqConstants.EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = ItemMqConstants.INSERT_KEY
    ))
    public void listenHotelInsert(Long itemId) {
        searchService.saveById(itemId);
    }

    // 监听 删除
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ItemMqConstants.DELETE_QUEUE_NAME),
            exchange = @Exchange(name = ItemMqConstants.EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = ItemMqConstants.DELETE_KEY
    ))
    public void listenHotelDelete(Long itemId){
        searchService.deleteById(itemId);
    }

}
